package com.itheima.service.impl;
/*
    ヾ(❀ﾟ▽ﾟ)ノ 
 
 Grey1996year@163.com 
 
*/

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.mapper.MemberMapper;
import com.itheima.mapper.OrderMapper;
import com.itheima.mapper.SetmealMapper;
import com.itheima.service.ReportService;
import com.itheima.util.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.*;

@Service(interfaceClass = ReportService.class)
public class ReportServiceImpl implements ReportService {

	@Autowired
	private MemberMapper memberMapper;

	@Autowired
	private OrderMapper orderMapper;

	@Autowired
	private SetmealMapper setmealMapper;

	@Override
	public Map getBusinessReportData() throws Exception {
		/*
			"data":{
					"reportDate":"2019‐04‐25",
					"todayNewMember":0,
					"totalMember":10,
					"thisWeekNewMember":0,
					"thisMonthNewMember":2,
					"todayOrderNumber":0,
					"todayVisitsNumber":0,
					"thisWeekOrderNumber":0,
					"thisWeekVisitsNumber":0,
					"thisMonthOrderNumber":2,
					"thisMonthVisitsNumber":0,

					"hotSetmeal":[
									{"proportion":0.4545,"name":"粉红珍爱(女)升级TM12项筛查体检套餐","setmeal_count":5},
									{"proportion":0.1818,"name":"阳光爸妈升级肿瘤12项筛查体检套餐","setmeal_count":2},
									{"proportion":0.1818,"name":"珍爱高端升级肿瘤12项筛查","setmeal_count":2},
									{"proportion":0.0909,"name":"孕前检查套餐","setmeal_count":1}
								]
					}
		 */
		HashMap<String, Object> hashMap = new HashMap<>();
		//reportDate
		Date dat = new Date();
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
		String date = simpleDateFormat.format(dat);
		//todayNewMember今日新增会员
		int todayNewMember = memberMapper.queryTodayNewMember(date);
		//totalMember 总会员数
		int totalMember = memberMapper.queryTotalMember();
		//thisWeekNewMember 本周新增会员数
		//获得本周一的日期
		String today = DateUtils.parseDate2String(DateUtils.getToday());
		int thisWeekNewMember = memberMapper.queryThisWeekNewMember(today);
		//thisMonthNewMember 本月新增会员数
		//获取本月第一天
		Date firstDay = DateUtils.getFirstDay4ThisMonth();
		String firstDay4ThisMonth = DateUtils.parseDate2String(firstDay);
		int thisMonthNewMember = memberMapper.queryThisMonthNewMember(firstDay4ThisMonth);
		//todayOrderNumber 今日预约人数
		int todayOrderNumber = orderMapper.queryTodayOrderNumber(date);
		//todayVisitsNumber 今日到诊数
		int todayVisitsNumber = orderMapper.queryTodayVisitsNumber(date);
		//thisWeekOrderNumber 这周预约人数
		int thisWeekOrderNumber = orderMapper.queryThisWeekOrderNumber(today);
		//thisWeekVisitsNumber 这周到诊人数
		int thisWeekVisitsNumber = orderMapper.queryThisWeekVisitsNumber(today);
		//thisMonthOrderNumber 这月预约人数
		int thisMonthOrderNumber = orderMapper.queryThisMonthOrderNumber(firstDay4ThisMonth);
		//thisMonthVisitsNumber 这月到诊人数
		int thisMonthVisitsNumber = orderMapper.queryThisMonthVisitsNumber(firstDay4ThisMonth);
		//热门套餐
		List<Map> map = setmealMapper.findHotSetmeal();

		hashMap.put("reportDate", date);
		hashMap.put("todayNewMember", todayNewMember);
		hashMap.put("totalMember", totalMember);
		hashMap.put("thisWeekNewMember", thisWeekNewMember);
		hashMap.put("thisMonthNewMember", thisMonthNewMember);
		hashMap.put("todayOrderNumber", todayOrderNumber);
		hashMap.put("todayVisitsNumber", todayVisitsNumber);
		hashMap.put("thisWeekOrderNumber", thisWeekOrderNumber);
		hashMap.put("thisWeekVisitsNumber", thisWeekVisitsNumber);
		hashMap.put("thisMonthOrderNumber", thisMonthOrderNumber);
		hashMap.put("thisMonthVisitsNumber", thisMonthVisitsNumber);
		hashMap.put("hotSetmeal",map);

		return hashMap;
	}
}
